<a href="<?php echo( base_url() ); ?>cmb/member_reports/download_balavihar_class_attendance_report/<?php echo( $urlKey ); ?>/" target="_self">Download Data</a>
<?php
	Includes::IncludeFile( Includes::$FORM );
	Includes::IncludeFile( Includes::$SPACER );
	
	$curdatetime = time();	
	$curdate = date('l, F jS, Y', $curdatetime);
	$numweeks = count($bvattinfo);
	$wk_block = 8;
	
	Form::OpenFormItemSummary('Date:', 50);
		echo( form_label($curdate, 'numrecs', 'style="width: 50px;" ') );
	Form::CloseFormItem();

	$curtime = date('h:i:s A', $curdatetime);
	Form::OpenFormItemSummary('Time:', 50);
		echo( form_label($curtime, 'numrecs', 'style="width: 50px;" ') );
	Form::CloseFormItem();

	Form::OpenFormItemSummary('School Year:', 260);
		echo( form_label($year.'-'.($year+1), 'numrecs', 'style="width: 20px;" ') );
	Form::CloseFormItem();
	
	if ($numweeks > 0)
	{
		$numstudents = count($bvattinfo[0]);
		
		Form::OpenFormItemSummary('Center:', 260);
			echo( form_label($centername, 'numrecs', 'style="width: 20px;" ') );
		Form::CloseFormItem();
		
		Form::OpenFormItemSummary('Session:', 260);
			echo( form_label($session, 'numrecs', 'style="width: 20px;" ') );
		Form::CloseFormItem();
		
		Form::OpenFormItemSummary('Grade:', 260);
			echo( form_label($gradename, 'numrecs', 'style="width: 20px;" ') );
		Form::CloseFormItem();
		
		Form::OpenFormItemSummary('Number of students:', 260);
			echo( form_label($numstudents, 'numrecs', 'style="width: 20px;" ') );
		Form::CloseFormItem();
	
		$i = 1;
		foreach ($teachers as $tvrec)
		{
			Form::OpenFormItemSummary('Teacher'.$i.':', 260);
				echo( form_label($tvrec, 'numrecs', 'style="width: 20px;" ') );
			Form::CloseFormItem();
			
			$i ++;						
		}
	}
	else
	{
		Form::OpenFormItemSummary('No results returned', 260);
		Form::CloseFormItem();			
	}

	echo('<hr style="width:50%; size:10; color:#0000FF;" align="left" />');
	
	Spacer::Render(0, 20);

	//Create output by month
	if (($numweeks > 0) and ($numstudents>0))
	{
		switch (intval($outputformat))
		{
			//Output by month
			case 1:
				$lastmonth = "";
				$iw = 0;
				
				if ($specmonth != "all")
				{
					$flg = true;
					do
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[0];
						$month = date("M",strtotime($bvclass[1]));
						$iw ++;
						if ($specmonth == strtolower($month)) $flg = false;
					} while (($iw < $numweeks) && $flg);
					$iw_beg = $iw - 1;
					
					do
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[0];
						$month = date("M",strtotime($bvclass[1]));
						$iw ++;
					} while (($iw < $numweeks) && ($specmonth == strtolower($month)));
					$iw_end = $iw - 1;
					
					//echo "IW_BEG: ".$iw_beg.", IW_END: ".$iw_end."</br>";
					//die;

					$bvwkrec = $bvattinfo[$iw_beg];
					$bvclass = $bvwkrec[0];
					$month = date("M",strtotime($bvclass[1]));
					echo('<table border="1">');
					echo('<tr>');
					echo('<th colspan="6"><center><font size="3" color="blue"><b>'.$month.'-'.$year.'</b></font></center></th>');
					echo('</tr>');
					echo('<tr>');
					echo('<th><center><b>&nbspStudent&nbspName&nbsp</b></center></th>');
					
					for($i = $iw_beg; $i < $iw_end; $i++)
					{
						$bvwkrec = $bvattinfo[$i];
						$bvclass = $bvwkrec[0];
						echo('<th><center><b>&nbsp'.$bvclass[1].'&nbsp</b></center></th>');
					}

					echo('</tr>');				

					for($ic = 0; $ic < $numstudents; $ic++)
					{
						if ($ic % 2 != 0)
						{
							$rowColor = "#e0ffff";
						}
						else
						{
							$rowColor = "#ffffff";
						}
						
						echo('<tr bgcolor="'.$rowColor.'">');
						$bvwkrec = $bvattinfo[0];
						$bvclass = $bvwkrec[$ic];
						if ($bvclass[6] == "unreg")
							echo('<td><font color="red">&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</font></td>');
						else
							echo('<td>&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</td>');
										
						for($i = $iw_beg; $i < $iw_end; $i++)
						{
							$bvwkrec = $bvattinfo[$i];
							$bvclass = $bvwkrec[$ic];
							if ($bvclass[4] == 0)
								$status = "-";
							else				
							{
								if ($bvclass[4] == 1)
									$status = "+";
								else
									$status = "";
							}
								
							if ($i <= ($curweekseq-1))
							{
								if ($bvclass[6] == "unreg")
									echo('<td><font color="red"><center>'.$status.'</center></font></td>');
								else
									echo('<td><center>'.$status.'</center></td>');
							}
							else
								echo('<td><center>&nbsp</center></td>');
						} //for($i = $iw_beg; $iw < $iw_end; $i++)
						
						echo('</tr>');
					} //for($ic = 0; $ic < $numstudents; $ic++)
					echo('</table>');
				}
				else
				{
					while ($iw < $numweeks)
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[0];
						$month = date("M",strtotime($bvclass[1]));
						$year = date("Y",strtotime($bvclass[1]));
						
						if ($month != $lastmonth)
						{
							$iw_beg = $iw;
							$lastmonth = $month;
							echo('<table border="1">');
							echo('<tr>');
							echo('<th colspan="6"><center><font size="3" color="blue"><b>'.$month.'-'.$year.'</b></font></center></th>');
							echo('</tr>');
							echo('<tr>');
							echo('<th><center><b>&nbspStudent&nbspName&nbsp</b></center></th>');
							do
							{
								echo('<th><center><b>&nbsp'.$bvclass[1].'&nbsp</b></center></th>');
								$iw ++;
								if ($iw < $numweeks)
								{
									$bvwkrec = $bvattinfo[$iw];
									$bvclass = $bvwkrec[0];
									$month = date("M",strtotime($bvclass[1]));
								}
							} while (($month == $lastmonth) && ($iw < $numweeks));
							echo('</tr>');				
						}
							
						$iw_end = $iw;
						
						for($ic = 0; $ic < $numstudents; $ic++)
						{
							if ($ic % 2 != 0)
							{
								$rowColor = "#e0ffff";
							}
							else
							{
								$rowColor = "#ffffff";
							}
							
							echo('<tr bgcolor="'.$rowColor.'">');
							$bvwkrec = $bvattinfo[0];
							$bvclass = $bvwkrec[$ic];
							if ($bvclass[6] == "unreg")
								echo('<td><font color="red">&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</font></td>');
							else
								echo('<td>&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</td>');
											
							for($i = $iw_beg; $i < $iw_end; $i++)
							{
								$bvwkrec = $bvattinfo[$i];
								$bvclass = $bvwkrec[$ic];
								if ($bvclass[4] == 0)
									$status = "-";
								else				
								{
									if ($bvclass[4] == 1)
										$status = "+";
									else
										$status = "";
								}
									
								if ($i <= ($curweekseq-1))
								{
									if ($bvclass[6] == "unreg")
										echo('<td><font color="red"><center>'.$status.'</center></font></td>');
									else
										echo('<td><center>'.$status.'</center></td>');
								}
								else
									echo('<td><center>&nbsp</center></td>');
							} //for($i = $iw_beg; $iw < $iw_end; $i++)
							
							echo('</tr>');
						} //for($ic = 0; $ic < $numstudents; $ic++)
						echo('</table>');
						Spacer::Render(0, 20);
					} //while ($iw < $numweeks)
				} //if ($specmonth != "all")
				break;
			
			//Output in blocks of 8 weeks
			case 2:
				$wkbeg = 0;
				$wk_block = 13;
				if ($specmonth != "all")
				{
					$iw = 0;
					$flg = true;
					do
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[0];
						$month = date("M",strtotime($bvclass[1]));
						$iw ++;
						if ($specmonth == strtolower($month)) $flg = false;
					} while (($iw < $numweeks) && $flg);
					$wkbeg = $iw - 1;
				}
				$actweeks = $numweeks - $wkbeg;
				if (($actweeks % $wk_block) != 0)
					$max_block = intval($actweeks / $wk_block) + 1;
				else
					$max_block = intval($actweeks / $wk_block);
				
				$max_week = $wkbeg + $wk_block;

				for($ib = 0; $ib < $max_block; $ib++)
				{
					echo('<table border="1">');
					echo('<tr>');
					echo('<th><center><b>&nbspStudent&nbspName&nbsp</b></center></th>');
		
					for($iw = $wkbeg; $iw < $max_week; $iw++)
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[0];
						echo('<th><center><b>&nbsp'.$bvclass[1].'&nbsp</b></center></th>');
					}
					echo('</tr>');			
			
					for($ic = 0; $ic < $numstudents; $ic++)
					{
						if ($ic % 2 != 0)
						{
							$rowColor = "#e0ffff";
						}
						else
						{
							$rowColor = "#ffffff";
						}
						
						echo('<tr bgcolor="'.$rowColor.'">');
						$bvwkrec = $bvattinfo[0];
						$bvclass = $bvwkrec[$ic];
						if ($bvclass[6] == "unreg")
							echo('<td><font color="red">&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</font></td>');
						else
							echo('<td>&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</td>');
										
						for($iw = $wkbeg; $iw < $max_week; $iw++)
						{
							$bvwkrec = $bvattinfo[$iw];
							$bvclass = $bvwkrec[$ic];
							if ($bvclass[4] == 0)
								$status = "-";
							else				
							{
								if ($bvclass[4] == 1)
									$status = "+";
								else
									$status = "";
							}
								
							if ($iw <= ($curweekseq-1))
							{
								if ($bvclass[6] == "unreg")
									echo('<td><font color="red"><center>'.$status.'</center></font></td>');
								else
									echo('<td><center>'.$status.'</center></td>');
							}
							else
								echo('<td><center>&nbsp</center></td>');
						}
						
						echo('</tr>');
					}
					echo('</table>');
					Spacer::Render(0, 20);
					
					$wkbeg += $wk_block;
					$max_week = ($wkbeg+$wk_block);
					if ($max_week > $numweeks) $max_week = $numweeks;
				}
				break;
			
			//Output all weeks
			case 3:
				$wkbeg = 0;
				if ($specmonth != "all")
				{
					$iw = 0;
					$flg = true;
					do
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[0];
						$month = date("M",strtotime($bvclass[1]));
						$iw ++;
						if ($specmonth == strtolower($month)) $flg = false;
					} while (($iw < $numweeks) && $flg);
					$wkbeg = $iw - 1;
				}

				echo('<table border="1">');
				echo('<th><center><b>&nbspStudent&nbspName&nbsp</b></center></th>');
				
				for($iw = $wkbeg; $iw < $numweeks; $iw++)
				{
					$bvwkrec = $bvattinfo[$iw];
					$bvclass = $bvwkrec[0];
					echo('<th><center><b>&nbsp'.$bvclass[1].'&nbsp</b></center></th>');
				}			
		
				for($ic = 0; $ic < $numstudents; $ic++)
				{
					if ($ic % 2 != 0)
					{
						$rowColor = "#e0ffff";
					}
					else
					{
						$rowColor = "#ffffff";
					}
					
					echo('<tr bgcolor="'.$rowColor.'">');
					$bvwkrec = $bvattinfo[0];
					$bvclass = $bvwkrec[$ic];
					if ($bvclass[6] == "unreg")
						echo('<td><font color="red">&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</font></td>');
					else
						echo('<td>&nbsp'.$bvclass[2].'&nbsp'.$bvclass[3].'&nbsp</td>');
					
					for($iw = $wkbeg; $iw < $numweeks; $iw++)
					{
						$bvwkrec = $bvattinfo[$iw];
						$bvclass = $bvwkrec[$ic];
						if ($bvclass[4] == 0)
							$status = "-";
						else				
						{
							if ($bvclass[4] == 1)
								$status = "+";
							else
								$status = "";
						}
							
						if ($iw <= ($curweekseq-1))
						{
							if ($bvclass[6] == "unreg")
								echo('<td><font color="red"><center>'.$status.'</center></font></td>');
							else
								echo('<td><center>'.$status.'</center></td>');
						}
						else
							echo('<td><center>&nbsp</center></td>');
					}
					
					echo('</tr>');
				}

				echo('</table>');
				break;
		}
	} //if (($numweeks > 0) and ($numstudents>0))
	else
		echo "No results returned";

/*
	//Create output in blocks of 8 weeks
	if (($numweeks > 0) and ($numstudents>0))
	{				
		$wkbeg = 0;
		$max_week = $wk_block;
		for($ib = 0; $ib < 5; $ib++)
		{
			echo('<table border="1">');
			echo('<tr>');
			echo('<th><center><b>Student&nbspName</b></center></th>');

			//for($iw = 0; $iw < $curweekseq; $iw++)
			for($iw = $wkbeg; $iw < $max_week; $iw++)
			{
				$bvwkrec = $bvattinfo[$iw];
				$bvclass = $bvwkrec[0];
				echo('<th><center><b>'.$bvclass[1].'</b></center></th>');
			}
			echo('</tr>');			
	
			for($ic = 0; $ic < $numstudents; $ic++)
			{
				if ($ic % 2 != 0)
				{
					$rowColor = "#e0ffff";
				}
				else
				{
					$rowColor = "#ffffff";
				}
				
				echo('<tr bgcolor="'.$rowColor.'">');
				$bvwkrec = $bvattinfo[0];
				$bvclass = $bvwkrec[$ic];
				if ($bvclass[6] == "unreg")
					echo('<td><font color="red">'.$bvclass[2].'&nbsp'.$bvclass[3].'</font></td>');
				else
					echo('<td>'.$bvclass[2].'&nbsp'.$bvclass[3].'</td>');
								
				for($iw = $wkbeg; $iw < $max_week; $iw++)
				//for($iw = 0; $iw < $curweekseq; $iw++)
				{
					$bvwkrec = $bvattinfo[$iw];
					$bvclass = $bvwkrec[$ic];
					if ($bvclass[4] == 0)
						$status = "-";
					else				
					{
						if ($bvclass[4] == 1)
							$status = "+";
						else
							$status = "";
					}
						
					if ($iw <= ($curweekseq-1))
					{
						if ($bvclass[6] == "unreg")
							echo('<td><font color="red"><center>'.$status.'</center></font></td>');
						else
							echo('<td><center>'.$status.'</center></td>');
					}
					else
						echo('<td><center>&nbsp</center></td>');
				}
				
				echo('</tr>');
			}
			echo('</table>');
			Spacer::Render(0, 20);
			
			$wkbeg += $wk_block;
			$max_week = ($wkbeg+$wk_block);
			if ($max_week > $numweeks) $max_week = $numweeks;
		}
	}
	else
		echo "No results returned";
*/	
?>
